package com.bifang.core.qrcode.gateway.controller;

import cn.hutool.core.convert.Convert;
import com.bifang.common.vo.*;
import com.bifang.core.qrcode.gateway.req.CodeCreateReq;
import com.bifang.core.qrcode.gateway.req.CodeDetailReq;
import com.bifang.core.qrcode.integration.AppCodeIntegration;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;
import sun.misc.BASE64Encoder;

@RestController
@RequestMapping(value = "/code")
@Api(tags = "码管理")
public class AppCodeController {
    @Autowired AppCodeIntegration appCodeIntegration;

    @PostMapping(value = "/getQRCode", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "获取二维码", notes = "获取二维码")
    public AppResponse<String> getQRCode(
            @RequestBody CodeCreateReq codeCreateReq, @ApiIgnore HttpServletResponse response) {
        CodeCreateVO codeCreateVO = Convert.convert(CodeCreateVO.class, codeCreateReq);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        BufferedImage bi = appCodeIntegration.getQRCode(codeCreateVO);
        try {
            ImageIO.write(bi, "png", output);
            byte[] bytes = output.toByteArray(); // 转换成字节
            BASE64Encoder encoder = new BASE64Encoder();
            String png_base64 = encoder.encodeBuffer(bytes).trim(); // 转换成base64串
            png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", ""); // 删除 \r\n
            return AppResponse.successData("data:image/png;base64," + png_base64);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (output != null) {
                    output.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return AppResponse.errorMsg("生成二维码异常");
    }

    @GetMapping(value = "/getQRCode")
    @ApiOperation(value = "获取二维码", notes = "获取二维码")
    public void getQRCode(
            @ApiIgnore BaseVO baseVO,
            @ApiParam("码属性：1 二维码 2 条形码") @RequestParam("codePro") String codePro,
            @ApiParam("码类型 20 个人身份码 30企业身份码") @RequestParam("codeType") String codeType,
            @ApiParam("码对应唯一id 个人码对应身份证 企业码对应统一社会信用编码") @RequestParam("uniqueId") String uniqueId,
            @ApiParam("码唯一id对应名称，如姓名或单位名称") @RequestParam("name") String name,
            @ApiParam("申请渠道类型 1-App  2-h5 3-微信小程序 4-支付宝小程序 5-钉钉小程序") @RequestParam("applyType")
                    String applyType,
            @ApiParam("码颜色：绿色 灰色 蓝色 红色") @RequestParam("codeColor") String codeColor,
            @ApiParam("是否只使用一次 0 否 1 是") @RequestParam("onceFlag") String onceFlag,
            @ApiIgnore HttpServletResponse response) {
        CodeCreateVO codeCreateVO = Convert.convert(CodeCreateVO.class, baseVO);
        codeCreateVO.setApplyType(applyType);
        codeCreateVO.setCodeColor(codeColor);
        codeCreateVO.setCodePro(codePro);
        codeCreateVO.setCodeType(codeType);
        codeCreateVO.setUniqueId(uniqueId);
        codeCreateVO.setName(name);
        codeCreateVO.setOnceFlag(onceFlag);

        ByteArrayOutputStream output = new ByteArrayOutputStream();
        BufferedImage bi = appCodeIntegration.getQRCode(codeCreateVO);
        ServletOutputStream out = null;
        try {
            ImageIO.write(bi, "png", output);
            out = response.getOutputStream();
            output.writeTo(out);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (output != null) {
                    output.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @PostMapping(value = "/getCodeInfo", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "获取二维码基本信息", notes = "获取二维码基本信息")
    public AppResponse<AppCodeVO> getCodeInfo(@RequestBody @Validated CodeDetailReq codeDetailReq) {
        CodeDetailVO codeDetailVO = Convert.convert(CodeDetailVO.class, codeDetailReq);
        return AppResponse.successData(appCodeIntegration.getCodeInfo(codeDetailVO));
    }

    @PostMapping(value = "/getCodeInfo/NOCheck", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "获取二维码基本信息", notes = "获取二维码基本信息")
    public AppResponse<AppCodeVO> getCodeInfoNOCheck(
            @RequestBody @Validated CodeDetailReq codeDetailReq) {
        CodeDetailVO codeDetailVO = Convert.convert(CodeDetailVO.class, codeDetailReq);
        return AppResponse.successData(appCodeIntegration.getCodeInfoNOCheck(codeDetailVO));
    }
}
